package com.wty.basic.IoDemo;


import org.junit.Test;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * 解压文件
 */
public class ZipDemo2 {



    //单文件的解压
    @Test
    public void test1() throws IOException {
        File zipFile=new File("src/main/resources/wtyZip.zip");
        String outDir="src/main/resources/unzip";
        unZip(zipFile,outDir);

    }

    //单文件夹多文件的解压
    @Test
    public void test2(){

    }


    //复合文件夹的解压
    @Test
    public void test3(){

    }


    private void unZip(File srcFile,String outDir) throws IOException {
        if(!srcFile.exists()){
            throw new RuntimeException("源文件不存在");
        }
        //读取源文件的流
        ZipInputStream zInput=new ZipInputStream(new FileInputStream(srcFile));
        ZipEntry zipEntry=null;

        while ((zipEntry=zInput.getNextEntry())!=null){
            String filePath=outDir+File.separator+zipEntry.getName();
            System.out.println(filePath);

//            File file=new File(outDir+File.separator+"aaaa"+File.separator+"bbbbbb"+File.separator+"a.txt");
//            if(!file.getParentFile().exists()){
//                file.getParentFile().mkdirs();
//            }
//            if(!file.exists()){
//                file.createNewFile();
//            }
            FileOutputStream out=new FileOutputStream(outDir+File.separator+"abcde"+File.separator+"a.txt");

            byte bytes[]=new byte[1024];
            int len=-1;
            while ((len=zInput.read(bytes))!=-1){
                out.write(bytes,0,len);
            }
            out.close();
        }
        zInput.close();


    }


}
